// pages/circle/circle.js
const app = getApp()
import { requst_get_model_follow, requst_get_model_unfollow,cirlrScore } from '../../api/modelList'
import { industry, experience, like } from '../../api/circle';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navBarHeight: app.globalData.navBarHeight,// 获取导航栏高度
    menuRight: app.globalData.menuRigh,// 获取胶囊距右方间距（方保持左、右间距一致）
    menuTop: app.globalData.menuTop,// 获取胶囊距底部间距（保持底部间距一致）
    menuHeight: app.globalData.menuHeight,// 获取胶囊高度（自定义内容可与胶囊高度保证一致）
    currentTab: 0,
    limits_id: '',
    tabs: [
      { label: '经验分享', index: 0 },
      { label: '业内资讯', index: 1 }
    ],
    modelEvaluate: [],
    newsList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;
    wx.getStorage({
      key: 'user',
      success(res) {
        that.setData({
          limits_id: res.data.limits_id
        })
      }
    })
    //屏幕信息
    wx.getSystemInfo({
      success: function (res) {
        // 高度,宽度 单位为px
        that.setData({
          windowWidth: res.windowWidth,
        })
      }
    })
  },

  likeFun: function (e) {  
    // is_like 0 未点赞状态   1 点赞状态   id 点赞id
    let id=e.currentTarget.dataset.item.id
    let index=e.currentTarget.dataset.index
    let status=e.currentTarget.dataset.item.is_like
    let modelEvaluate=this.data.modelEvaluate
    let that=this
    // console.log(id,index,status,modelEvaluate,'e===========e.currentTarget.dataset')
    like({ experience_sharing_id: id, type: status == 1 ? 2 : 1 }).then(
      (res)=>{
        if(res.errCode==0){
          if(modelEvaluate[index].is_like===0){
            modelEvaluate[index].is_like=1
            modelEvaluate[index].like_count=modelEvaluate[index].like_count+1
          }else{
            modelEvaluate[index].is_like=0
            modelEvaluate[index].like_count=modelEvaluate[index].like_count-1
          }
          that.setData({
            modelEvaluate
          })
        }
      }
    )
    
  },
  async follow(e) {
    await requst_get_model_follow({ mid: e.currentTarget.dataset.bean.users_uid })
    this.getNewsListList();
    this.getModelEvaluateList();
    wx.showToast({ title: '关注成功！', icon: 'none' });
  },
  async unfollow(e) {
    console.log(e.currentTarget.dataset.bean, '123')
    await requst_get_model_unfollow({ mid: e.currentTarget.dataset.bean.users_uid })
    this.getNewsListList();
    this.getModelEvaluateList();
    wx.showToast({ title: '取消成功！', icon: 'none' });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    //自定义的tabbar
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 2
      })
    }
    //  设置定时器
    this.timer = setInterval(this._cirlrScore, 15000); 
    this.getNewsListList();
    this.getModelEvaluateList();
  },
  async _cirlrScore(){
    await cirlrScore()
    console.log('添加成功')
  },
  getNewsListList(page) {
    industry({
      page: page ? page : 1,
      limit: 15,
    })
      .then(res => {
        let arr = [], newsList = [];
        if (res.data && res.data.data && res.data.data.length) {
          res.data.data.forEach(element => {
            arr.push({
              id: element.id,
              url: element.banner_url,
              title: element.title,
              info: element.content.replace('<p>', '').replace('</p>', ''),
              time: element.created_at,
            })
          });
        }
        this.setData({
          newsList: arr
        })
      })
  },
  getModelEvaluateList(page) {
    experience({
      page: page ? page : 1,
      limit: 15,
    })
      .then(res => {
        let arr = [], modelEvaluate = [];
        if (res.data && res.data.length) {
          res.data.forEach(element => {
            arr.push({
              ...element,
              id: element.id,
              url: element.avatar,
              name: element.examine_at,
              time: element.created_at,
              content: element.experience_content,
              pictures: element.picture
            })
          });
        }
        if (page > 1) {
          modelEvaluate = [].concat(this.data.modelEvaluate, arr)
        } else {
          modelEvaluate = arr;
        }
        this.setData({
          modelEvaluate: modelEvaluate
        })
        console.log(res, 'getModelEvaluateList', modelEvaluate)
      })
  },
  // tab切换
  changeCurrentTab_(e) {
    let that = this
    if (that.data.currentTab === e.currentTarget.dataset.current) {
      return false
    } else {
      that.setData({
        currentTab: e.currentTarget.dataset.current
      })
    }
  },
  //滑动切换
  swiperTab: function (e) {
    var that = this;
    that.setData({
      currentTab: e.detail.current
    });
  },
  // 咨讯详情
  moveToNewDetail(event) {
    const item = event.currentTarget.dataset.item;
    console.log(item, 'moveToNewDetail item')
    if (item) {
      wx.navigateTo({
        url: '/subPackages/pages/circleInformationDetails/circleInformationDetails?id=' + item.id,
      })
    }
  },
  // 经验详情 
  moveToEvaluateDetail(event) {
    const item = event.currentTarget.dataset.item;
    if (item) {
      wx.navigateTo({
        url: '/subPackages/pages/circleExperienceDetails/circleExperienceDetails?id=' + item.id,
      })
    }
  },
  moveToAdd() {
    wx.navigateTo({
      url: '/subPackages/pages/addExperience/addExperience',
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    clearInterval(this.timer); // 停止定时器
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})